<template>
    <div class="wrapper">
        <div id="app">
            <h1>对话应用</h1>
            <div id="chat">
                <div v-for="msg in messages" :class="msg.role">
                    {{ msg.role === 'user' ? '我：' : '小百科：' }}{{ msg.content }}
                </div>
            </div>
            <div class="input-box">
            <div class="input-header">
                <img src="../assets/img/pic.png" >
                <img src="../assets/img/wenjian.png" >
                <img src="../assets/img/yuyin.png" >
            </div>
            <input type="text" v-model="userMessage" placeholder="输入您的消息" @keypress="handleKeyPress">
            <img class="send" src="../assets/img/fasong.png" style="padding-top: 10px;"  @click="sendMessage"> 
            <!-- <button @click="sendMessage"><img src="../assets/img/fasong.png" ></button> -->
        </div>
        </div>

        <div class="bottom-ban">
            <Footer></Footer>
        </div>
    </div>
</template>


<script>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import Footer from '../components/tijianfooter.vue';
import { nextTick } from 'vue';
import axios from 'axios';





export default {
    setup() {
        const router = useRouter();
        const userMessage = ref('');
        const messages = ref([]);

        function sendMessage() {
            if (userMessage.value.trim() === '') return;

            const message = userMessage.value;
            messages.value.push({ role: 'user', content: message });
            userMessage.value = '';

            axios.post('/spa/chat', { message })
                .then(response => {
                    if (response.data.response) {
                        messages.value.push({ role: 'bot', content: response.data.response });
                    } else {
                        messages.value.push({ role: 'bot', content: '错误：' + response.data.error });
                    }

                    nextTick(() => {
                        const chat = document.getElementById('chat');
                        chat.scrollTop = chat.scrollHeight; // 滚动到最新消息
                    });
                })
                .catch(error => {
                    console.error('Error:', error);
                    messages.value.push({ role: 'bot', content: '请求失败' });
                });
        }
        function handleKeyPress(event) {
            if (event.key === 'Enter') {
                sendMessage();
            }
        }

        return {
            userMessage,
            messages,
            sendMessage,
            handleKeyPress
        };
    },
    components: {
        Footer
    }
}
</script>
<style scoped>
/*********************** 总容器 ***********************/
.wrapper {
    width: 100%;
    height: 100%;
    background-image: url(../assets/img/R.jpg);
    background-color: rgba(250, 250, 250, 0.877);
    align-items: center;
    justify-content: center;
    display: flex;
}

body {
    font-family: Arial, sans-serif;
}

#chat {
    margin-left: 30px;
    height: 600px;
    overflow-y: scroll;
    border: 1px solid #ccc;
    padding: 10px;
    background-color: #F9F9F9;
}

.user {
    color: rgb(10, 13, 201);
    font-weight: bold;
    font-size: larger;
}

.bot {
    color: rgb(0, 0, 0);
    font-weight: bold;
    font-size: larger;
}

.input-box {
    width: 90%;
    height: 200px;
    margin: 5%;
    border-radius: 10px; 
     box-sizing: border-box;
    border:2px solid rgb(8, 0, 0);
}

.input-header {
    height: 70px;
    background-color: #F9F9F9;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    text-align: center;
    font-size: 20px;
    font-weight: bold;
}

input{
    padding-left:5% ;
    width: 80%;
    height: 100px;
    border: #ccc;
    text-align: center;
    outline: none;
}

.input-header img {
    width: 30px;
    height: 30px;
    margin-top: 20px;
    margin-left: 10px;
    margin-right: 40px; 
}

h1 {
    padding-top: 5%;
    text-align: center; /* 设置文本为居中 */
}
.send{
    width: 30px;
    height: 30px;
}
.sand img{
    /* padding: 10px; */

    width: 60px;
    height: 60px;
}
</style>